﻿using DevExpress.Web.ASPxUploadControl;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using Utility1;

namespace eDream
{
    public partial class demo : System.Web.UI.Page
    {
        public string pathUrl = "";
        const string UploadDirectory = "~/TaiLieu/Msmart/";
        protected void Page_Load(object sender, EventArgs e)
        {
            pathUrl = WebUtils.pathUrl() + "/TaiLieu/Msmart/";
        }

        protected void uplImage_FileUploadComplete(object sender, FileUploadCompleteEventArgs e)
        {
            e.CallbackData = SavePostedFile(e.UploadedFile, 0, 290);
        }

        string SavePostedFile(UploadedFile uploadedFile, int index, int size)
        {
            if (uploadedFile.FileName == "")
                return string.Empty;

            // Random r = new Random(9999);
            // int n = r.Next(999999);
            string ThumbnailFileName = uploadedFile.FileName;
            string fileName = Path.Combine(MapPath(UploadDirectory), ThumbnailFileName);
            //lbthongbao.Text = fileName;
            using (Image original = Image.FromStream(uploadedFile.FileContent))
            using (Image thumbnail = PhotoUtils.Inscribe(original, size))
            {
                PhotoUtils.SaveToJpeg(thumbnail, fileName);
            }
            switch (index)
            {
                case 0:
                    Session["link0"] = ThumbnailFileName;
                    break;
                case 1:
                    Session["link1"] = ThumbnailFileName;
                    break;
                case 2:
                    Session["link2"] = ThumbnailFileName;
                    break;
                case 3:
                    Session["link3"] = ThumbnailFileName;
                    break;
                case 4:
                    Session["link4"] = ThumbnailFileName;
                    break;
                case 5:
                    Session["link5"] = ThumbnailFileName;
                    break;
                case 6: Session["link6"] = ThumbnailFileName;
                    break;
            }
            Session.Timeout = 120;
            return ThumbnailFileName;
        }

        string SavePostedFile(System.Web.UI.WebControls.FileUpload uploadedFile)
        {
            if (uploadedFile.FileName == "")
                return string.Empty;

            Random r = new Random(9999);
            int n = r.Next(999999);
            string ThumbnailFileName = n + FileUpload1.FileName;
            string fileName = Path.Combine(MapPath(UploadDirectory), ThumbnailFileName);
            using (Image original = Image.FromStream(uploadedFile.FileContent))
            using (Image thumbnail = PhotoUtils.Inscribe(original, 200))
            {
                PhotoUtils.SaveToJpeg(thumbnail, fileName);
            }
            return ThumbnailFileName;
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (FileUpload1.HasFile)
            {
                string file = SavePostedFile(FileUpload1);
                Label1.Text = file;
            }
        }

        protected void ASPxButton1_Click(object sender, EventArgs e)
        {
            if (ASPxUploadControl1.HasFile)
            {
                string file = SavePostedFile3(ASPxUploadControl1);
                Label1.Text = file;
            }
        }

        string SavePostedFile3(ASPxUploadControl uploadedFile)
        {
            if (uploadedFile.FileName == "")
                return string.Empty;

            Random r = new Random(9999);
            int n = r.Next(999999);
            string ThumbnailFileName = n + FileUpload1.FileName;
            string fileName = Path.Combine(MapPath(UploadDirectory), ThumbnailFileName);
            using (Image original = Image.FromStream(uploadedFile.FileContent))
            using (Image thumbnail = PhotoUtils.Inscribe(original, 200))
            {
                PhotoUtils.SaveToJpeg(thumbnail, fileName+".jpg");
            }
            return ThumbnailFileName;
        }

        void Page_PreRender()
        {

            if (Session["btn"] == "1")
            {
                string upFolder = MapPath("~/TaiLieu/");

                DirectoryInfo dir = new DirectoryInfo(upFolder);

                dlstImages.DataSource = dir.GetFiles();

                dlstImages.DataBind();
            }
            else if (Session["btn"] == "2")
            {
                Page_PreRender2();
            }
            else
            Page_PreRender3();
        }

        void Page_PreRender2()
        {

            string upFolder = MapPath("~/TaiLieu/Msmart/");

            DirectoryInfo dir = new DirectoryInfo(upFolder);

            DataList1.DataSource = dir.GetFiles();

            DataList1.DataBind();

        }

        void Page_PreRender3()
        {

            string upFolder = MapPath("~/TaiLieu/Thum/");

            DirectoryInfo dir = new DirectoryInfo(upFolder);

            DataList2.DataSource = dir.GetFiles();

            DataList2.DataBind();

        }
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            Session["btn"] = "2";
            if (upImage.HasFile)
            {

                if (CheckFileType(upImage.FileName))
                {

                    String filePath = "~/TaiLieu/Msmart/" + upImage.FileName;

                    upImage.SaveAs(MapPath(filePath));

                }

            }

        }

        protected void btnAdd1_Click(object sender, EventArgs e)
        {
            Session["btn"] = "1";
            if (upImage.HasFile)
            {

                if (CheckFileType(upImage.FileName))
                {

                    String filePath = "~/TaiLieu/" + upImage.FileName;

                    upImage.SaveAs(MapPath(filePath));

                }

            }

        }

        bool CheckFileType(string fileName)
        {

            string ext = Path.GetExtension(fileName);

            switch (ext.ToLower())
            {

                case ".gif":

                    return true;

                case ".png":

                    return true;

                case ".jpg":

                    return true;

                case ".jpeg":

                    return true;

                default:

                    return false;

            }

        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            Session["btn"] = "3";
            if (upImage.HasFile)
            {

                if (CheckFileType(upImage.FileName))
                {

                    String filePath = "~/TaiLieu/Thum/" + upImage.FileName;

                    upImage.SaveAs(MapPath(filePath));

                }

            }
        }
    }
}